#include "head.h"
void* threadFunc(void* arg){
    threadPool_t* pthreadPool=(threadPool_t*)arg;
    while(1){
        pthread_mutex_lock(&pthreadPool->mutex);
        while(pthreadPool->taskQueue.queueSize==0 && pthreadPool->exitflag==0){
            pthread_cond_wait(&pthreadPool->cond,&pthreadPool->mutex);
        }
        if(pthreadPool->exitflag==1){
            pthread_mutex_unlock(&pthreadPool->mutex);
            pthread_exit(NULL);
        }
        printf("子线程获取一个任务\n");
        int netfd=pthreadPool->taskQueue.front->netfd;
        dequeue(&pthreadPool->taskQueue);
        pthread_mutex_unlock(&pthreadPool->mutex);
        //做任务
        //客户端发来用户名和密码，服务端收到
        char name[128]={0};
        char buf[1024]={0};

        //ret 0未出错1出错
        int ret;
        printf("正在执行login\n");
        login(netfd,name);
        printf("login执行结束\n");
        
        stack1_t pathstack;
        bzero(&pathstack,sizeof(pathstack));//这里作为压栈保存文件地址
        sprintf(buf,"/home/chenhang/%s",name);
        printf("worker.c: %s\n",buf);
        push(&pathstack,buf);

        while(1){
            int opt=99;
            int arg_length;
            ret=recvn(netfd,&opt,sizeof(int));
            if(ret==0){
                break;
            }
            printf("operate num= %d\n",opt);
            printf("ret= %d\n",ret); 
            //接受其他参数
            ret=recvn(netfd,&arg_length,sizeof(int));
            if(ret==0){
                break;
            }
            printf("ret= %d\n",ret);
            char* arg=(char*)malloc(sizeof(char)*(arg_length+1));
            ret=recvn(netfd,arg,arg_length);
            arg[arg_length]='\0';
            printf("ret= %d\n",ret);
            if(arg[0]!='\0'){
                arg[arg_length-1]='\0';
            }
            printf("argnum= %d arg= %s\n",arg_length,arg);
            switch(opt){
            case 0:
                //cd
                ret=cdFunc(arg,&pathstack);
                print_stack(pathstack);
                LOG_CHECK("cd",arg);
                printf("send is ready\n");
                send(netfd,&ret,sizeof(ret),MSG_NOSIGNAL);//向客户端反应
                
                break;
            case 1:
                //ls
                lsFunc(arg,netfd,&pathstack);
                LOG_CHECK("ls",arg);
                break;
            case 2:
                //puts
                putsText(arg,netfd,pathstack);
                LOG_CHECK("puts",arg);
                break;
            case 3:
                //gets
            case 4:
                //remove
                ret=removeFunc(arg,pathstack);
                LOG_CHECK("remove",arg);
                send(netfd,&ret,sizeof(int),MSG_NOSIGNAL);
                break;
            case 5:
                //pwd
                pwdFunc(netfd,pathstack);
                LOG_CHECK("pwd",arg);
                break;
            case 6:
                //mkdir
                ret=mkdirFunc(arg,pathstack);
                LOG_CHECK("mkdir",arg);
                send(netfd,&ret,sizeof(int),MSG_NOSIGNAL);
                break;
            case 7:
                //rmdir
                ret=rmdirFunc(arg,pathstack);
                LOG_CHECK("rmdir",arg);
                send(netfd,&ret,sizeof(int),MSG_NOSIGNAL);
                break;
            default:
                send(netfd,&ret,sizeof(int),MSG_NOSIGNAL);
            }
            
        }
    }
}
//创建线程
int makeWorker(threadPool_t * pthreadPool){

    for(int i=0;i<pthreadPool->workerNum;i++){

       int ret= pthread_create(&pthreadPool->tidArry[i],NULL,threadFunc,pthreadPool);
        if(ret!=0){
            error(1,errno,"pthread create is error\n");
        }
    }
    return 0;
}
